import { addArtDao, delArtDao, getArtByIdDao, getArtByPageDao, updateArtDao } from "../Dao/artDao.js"
import { getTypeByIdDao } from "../Dao/typeDao.js"
import { findUserByIdDao } from "../Dao/userDao.js"

export const addArtServ = async (artobj) => {
    await addArtDao(artobj)
    return {
        code: 0,
        msg: 'add art'
    }
}
export const findArtByPageServ = async(page, size, queryobj) => {
    const res = await getArtByPageDao(page, size, queryobj);
    res.list = await Promise.all(res.list.map(async item => {
        const obj = item.toObject(); // mongo对象转化为普通对象
        obj.type = await getTypeByIdDao(item.typeId)
        obj.user = await findUserByIdDao(item.userId)
        return obj
    }));
    
    return {
        code: 0,
        data: res,
    }
}

export const delArtServ = async (id) => {
    return await delArtDao(id)
}

export const updateArtServ = async (id, updateobj) => {
    return await updateArtDao(id,updateobj)
}

export const findArtByIdServ = async (id) => {
    const art = await getArtByIdDao(id);
    //
    const data = art.toObject();
    data.user = await findUserByIdDao(data.userId)
    data.type = await getTypeByIdDao(data.typeId)
    return{
        code: 0,
        data
    }
}
